<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/classes/BaseComponent.class.php, $Author:  $ */

/** Base class for all module's components
 *
 * @package module
 */
class BaseComponent extends Object
{

    /** Owner module
     *
     * @var BaseModule
     */
    var $_oModule = null;

    /**  Module parameters
     *
     * @var array
     */
    var $_aParams = array();

    /** Constructor
     *
     * @param BaseModeul $oModule owner module
     * @param array      $A owner module
     */
    function BaseComponent($oModule, $aParams = array())
    {
        parent::Object();
        $this->_oModule = $oModule;
        $this->_aParams = $aParams;
        $this->oDatabase = &Database::get();
        $this->oTpl = &$this->_oModule->getTemplate();
    }

    /** Processing action
     *
     * @return string
     */
    function process()
    {
        return '';
    }

    /** Save output to component cache
     *
     * @param string $sHtml component output
     */
    function saveToHtmlCache($sHtml)
    {
        $sFilePath = $this->_getHtmlCachePath();
        BaseComponent::_makeDir(dirname($sFilePath));
        $hCache = fopen($sFilePath, 'w');
        fwrite($hCache, $sHtml);
        fclose($hCache);
    }

    /** Try to get block output from cache. Returns null if no cached output
     *
     * @return string cahced output or null if no cached date found
     */
    function getFromHtmlCache()
    {
        $oReq = &App::getRequest();
        /* @var $oReq Request */
        if ($oReq->getAction())
            return null;
        $sRes = null;
        $sFilePath = $this->_getHtmlCachePath();
        if (file_exists($sFilePath))
        {
            if (filectime($sFilePath) < (time() - $this->_oModule->_iTime))
                return null;
            $sRes = file_get_contents($sFilePath);
        }
        return $sRes;
    }

    /** Returns file name for cache file
     *
     * @return string
     */
    function _getHtmlCachePath()
    {
        return $this->_oModule->getHtmlCacheDir(get_class($this)).App::getPageName().'.html';
    }

    /** Make recursive dir
     *
     * @static
     * @param string $sPath dir path
     */
    function _makeDir($sPath)
    {
        if (!file_exists($sPath))
        {
           BaseComponent::_makeDir(dirname($sPath));
           mkdir($sPath);
        }
    }

    function GetTplData($sName)
    {
    	if ( isset($this->oTpl->_tpl_vars[$sName]) )
    	{
    		return $this->oTpl->_tpl_vars[$sName];
    	}

    	return;
    }

    /** Return parameter value (or default value if no parameter set)
     *
     * @param string  $sName     parameter
     * @param mixed   $sDeafult  default value
     * @return mixed parameter value (or deafult value if given parameter was not set up)
     */
	function getParam($sName, $sDefault = '')
	{
		if (isset($this->_aParams[$sName]))
		{
			return $this->_aParams[$sName];
		}
		else
		{
			return $sDefault;
		}
	}
}
?>